package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.User;
import service.CheckUsernameByAjaxService;

/**
 * 判断用户名是否被占用
 */
public class CheckUsernameByAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码
		//获取username
		String username=request.getParameter("username");
		System.out.println(username);
		System.out.println("hhahhah");
		
		//调用service中方法,返回user
		User user = null;
		try {
			user =new  CheckUsernameByAjaxService().checkuserByUsername(username);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//判断user是否为空
		if(user==null){
		//如果为空，那就是输出1，用户名可以用
			response.getWriter().println("1");
		}else{
		//如果不为空那就输出0，用户名不能用
			response.getWriter().println("0");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
